using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using LispInterpretor.Parsers;
using LispInterpretor.HelperClasses;

namespace LispInterpretor
{
    public static class Evaluator
    {
        private static IParser[] Parsers = new IParser[] { 
                                                            new BoolParser(), 
                                                            new NumberParser(), 
                                                            new StringParser(),
                                                            new ProcedureParser(),
                                                            new ValueParser(),
                                                            new IfStatementParser(),
                                                            new CondParser(),
                                                            new LetParser(),
                                                            new DefineParser(),
                                                            new LambdaParser(),
                                                            new ListParser()
                                                          };

        public static object Eval(string text)
        {
            return Eval(text, new Environment());
        }

        public static object Eval(string text, Environment environment)
        {
            string whiteSpaceCleared = ParserHelper.ClearWhiteSpace(text);

            foreach (IParser parser in Parsers)
            {
                if (parser.Is(whiteSpaceCleared, environment))
                {
                    return parser.Evaluate(text, environment);
                }
            }

            throw new InvalidProgramException("Unable to parse LISP program");
        }

    }
}
